package com.fruit.web.vo.common;

import java.util.ArrayList;


import java.util.List;

import javax.faces.model.SelectItem;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.fruit.hibernate.dao.support.Page;
import com.fruit.service.IConsultationService;
import com.fruit.utils.SpringFacesUtil;
import com.fruit.web.bo.impl.Consultation;
import com.fruit.web.constant.SpringBeanConstants;

public class ConsultationPage implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224750L;
	private Page<Consultation,Long> page = new Page<Consultation,Long>();
	private SelectItem[] pageItems = null ;
	private Integer pageSize ;
	private Integer pageNo ;
	private Consultation param;
	public Page<Consultation, Long> getPage() {
		return page;
	}
	public void setPage(Page<Consultation, Long> page) {
		this.page = page;
	}
	public void getPageDatas() {//产品关联的所有相关咨评1�7
		Page<Consultation,Long> curPage = new Page<Consultation,Long>();
		Integer pPageNo = this.getPageNo();
		if (null == pPageNo) {
			pPageNo = new Integer(1);
		}
		Integer pPageSize = this.getPageSize();
		if (null == pPageSize) {
			pPageSize = new Integer(5);
		}
		curPage = this.getData(pPageNo, pPageSize, this.getParam());
		SelectItem[] tempPageItem = new SelectItem[(int)curPage.getTotalPageCount()];
		for (int j=0;j<tempPageItem.length;j++) {
			tempPageItem[j]= new SelectItem(new Integer(j+1),String.valueOf(j+1));
		}
		this.setPageItems(tempPageItem);
		this.setPage(curPage);
	}
	public void getPageDatasForCustomer() {//某固定客户某种状态的咨询
		Page<Consultation,Long> curPage = new Page<Consultation,Long>();
		Integer pPageNo = this.getPageNo();
		if (null == pPageNo) {
			pPageNo = new Integer(1);
		}
		Integer pPageSize = this.getPageSize();
		if (null == pPageSize) {
			pPageSize = new Integer(10);
		}
		curPage = this.getDataByCustomer(pPageNo, pPageSize, this.getParam());
		this.setPage(curPage);
	}
	public SelectItem[] getPageItems() {
		return pageItems;
	}
	private static SelectItem[] pageSizeItems = {
		new SelectItem(new Long(5),"5"),
		new SelectItem(new Long(10),"10"),
	};
	public SelectItem[] getPageSizeItems() {
		return pageSizeItems;
	}

	public void setPageSizeItems(SelectItem[] pageSizeItems) {
		ConsultationPage.pageSizeItems = pageSizeItems;
	}
	public void setPageItems(SelectItem[] pageItems) {
		this.pageItems = pageItems;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getPageNo() {
		return pageNo;
	}
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}
	public Consultation getParam() {
		return param;
	}
	public void setParam(Consultation param) {
		this.param = param;
	}
	public ConsultationPage(Integer pageNo, Integer pageSize, Consultation param) {
		super();
		this.pageSize = pageSize;
		this.pageNo = pageNo;
		this.param = param;
		
	}
	private Page<Consultation,Long> getData(int pageNo, int pageSize, Consultation param) {
		IConsultationService<Consultation,Long> consultationService = (IConsultationService<Consultation,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.CONSULTATION_SERVICE_BEAN_NAME);
		consultationService.setEntityClass(Consultation.class);
		consultationService.setPKClass(Long.class);
		String defaultHql = "from Consultation where 1=1 ";
		String hql = "from Consultation where 1=1 ";
		
		List paraList = new ArrayList();
		if (null != param) {// only use code type status showOnPage
			
			if (null != param.getCode() && !("".equals(param.getCode()))) {
				hql = hql + " and code=? ";
				paraList.add(param.getCode());
			}
			if (null != param.getTypeCode() && !("".equals(param.getTypeCode()))) {
				hql = hql + " and typeCode=? ";
				paraList.add(param.getTypeCode());
			}
			if (null != param.getStatusCode() && !("".equals(param.getStatusCode()))) {
				hql = hql + " and statusCode=? ";
				paraList.add(param.getStatusCode());
			}
			hql = hql + " and showOnPage=? ";
			paraList.add(param.isShowOnPage());
			
		}
		
		Object[] values = new Object[paraList.size()];
		for (int i = 0; i< values.length;i++) {
			values[i] = paraList.get(i);
		}
		Page<Consultation,Long> resultPage = consultationService.pagedQuery(hql, pageNo, pageSize,values); 
		return resultPage;
	}
	
	
	private Page<Consultation,Long> getDataByCustomer(int pageNo, int pageSize, Consultation param) {// param is statuscode and customer
		IConsultationService<Consultation,Long> consultationService = (IConsultationService<Consultation,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.CONSULTATION_SERVICE_BEAN_NAME);
		consultationService.setEntityClass(Consultation.class);
		consultationService.setPKClass(Long.class);
		Criteria criteria = consultationService.createCriteria(null);
		if (null != param.getStatusCode() && !("".equals(param.getStatusCode()))) {
			criteria.add(Restrictions.eq("statusCode", param.getStatusCode()));
		}
		if (null != param.getCustomer() && (null != param.getCustomer().getCustomerId() && !("".equals(param.getCustomer().getCustomerId())))) {
			criteria.createCriteria("customer").add(Restrictions.eq("customerId", param.getCustomer().getCustomerId()));
		}
		Page<Consultation,Long> resultPage = consultationService.pagedQuery(criteria, pageNo, pageSize); 
		return resultPage;
	}
}
